package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.model.Comment;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 我的评论Adapter
 * Created by wjy on 2018/10/12.
 */
public class MyCommentListItemAdapter extends BaseListAdapter<Comment, MyCommentListItemAdapter.ViewHolder> {

    private boolean mRevert = true;
    private int mAllNumber;

    public MyCommentListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    public void setAllNumber(int number) {
        mAllNumber = number;
    }

    public void setRevert(boolean isRevert) {
        mRevert = isRevert;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_person_comment_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Comment comment = mData.get(position);
        if (position == 0) {
            holder.year.setVisibility(View.VISIBLE);
            holder.year.setText(getYear(comment.getCommentTime()) + "年");
        } else {
            holder.year.setVisibility(View.GONE);
        }
        holder.title_date.setText(getDate(comment.getCommentTime()));
        holder.comment.setText(comment.getCommentId() != 0 ? "回复了" : "评论了");
        holder.content.setText(comment.getContent());
        holder.content_lay.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onLongClickLinstener!=null){
                    if(comment.getUser().getId()== UserManager.getInstance().getUser().getId()){
                        onLongClickLinstener.onLongClick(Long.parseLong(String.valueOf(comment.getId())));
                    }

                }
                return false;
            }
        });

        if(comment.getUser() != null){
            holder.name.setText(comment.getUser().getNickname()==null?"":comment.getUser().getNickname());
        }

    }

    private String getArticleType(Article article) {
        if (article.isVideo()) return "视频";
        else if (article.getType() == 1) return "日记";
        else return "话题";
    }

    private String getYear(String time) {
        try {
            return time.substring(0, 4);
        } catch (Exception e) {
            return "2019";
        }
    }

    private String getDate(String time) {
        try {
            String date = time.split(" ")[0];
            String number[] = date.split("-");
            return number[1] + "月" + number[2] + "日";
        } catch (Exception e) {
            return "";
        }
    }
    public interface OnLongClickLinstener{
        void onLongClick(Long id);
    }
    public OnLongClickLinstener onLongClickLinstener;
    public void setOnLongClickLinstener(OnLongClickLinstener onLongClickLinstener){
        this.onLongClickLinstener=onLongClickLinstener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, title_date, content, year, comment;
        LinearLayout imageLayout;
        ImageView image1, image2, image, play;
        RoundedImageView head;
        RelativeLayout content_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            year = itemView.findViewById(R.id.tv_year);
            title_date = itemView.findViewById(R.id.tv_date_title);
            name = itemView.findViewById(R.id.tv_name);
            comment = itemView.findViewById(R.id.tv_comment);
            content = itemView.findViewById(R.id.tv_content);
            content_lay = itemView.findViewById(R.id.content_lay);
        }
    }


}
